//add select folder
function addSelectOption() {
	addPathSelect("#chooseRelationFile", "relationPathSelect");
	addPathSelect("#chooseUploadFile", "uploadPathSelect");

}

// add view table
function createTableForView(divName, tableID) {
	var tableStructure = "<table  class='table table-bordered' id='" + tableID + "'><tr><th>File</th><th>View</th></tr></table>";

	$("#" + divName).append(tableStructure);

}

// function for select single checkbox
function bindUploadFile() {

	bindCheckBox($(".relation-file"));

}

function bindCheckBox(group) {
	group.click(function() {
		var $box = $(this);
		if ($box.is(":checked")) {
			group.prop("checked", false);
			$box.prop("checked", true);
		} else {
			$box.prop("checked", false);
		}
	})
}

// checkbox choose function
function selectAll(name) {
	$("#selectAll").click(function() {
		debugger
		name.prop("checked", true);
	})

}

function unSelectAll(name) {
	$("#unSelectAll").click(function() {
		name.prop("checked", false);
	})

}

function reverse(name) {
	$("#reverse").click(function() {
		name.each(function() {
			debugger
			if ($(this).attr("checked") == "false")
				$(this).prop("checked", true);
			else
				$(this).prop("checked", false);
		});
	})

}

// view relation files
function viewRelationFile() {
	$("#showRelationBtn").click(
			function() {
				$("#viewRelationFiles").html("");
				$.ajax({
					url : "../rest/csv/viewAllFiles",
					data : {
						"path" : $("#relationPathSelect").val()
					},
					dataType : "json",
					method : "post",
					success : function(data) {
						createTableForView("viewRelationFiles", "relationTable");
						$.each(data, function(k, v) {
							$("#relationTable").append(
									"<tr><td>" + v + "</td><td><input type='checkbox' class='relation-file'  value=" + v
											+ "></td>" + "</tr>");
							bindUploadFile();
						});
						
					}
				});

			})
}

// view upload file
function viewUploadFile() {
	$("#showUploadBtn").click(
			function() {
				$("#viewUploadFiles").html("");
				$.ajax({
					url : "../rest/csv/viewAllFiles",
					data : {
						"path" : $("#uploadPathSelect").val()
					},
					dataType : "json",
					method : "post",
					success : function(data) {
						createTableForView("viewUploadFiles", "uploadTable");
						$.each(data, function(k, v) {
							$("#uploadTable")
									.append(
											"<tr><td>" + v + "</td><td><input type='checkbox' class='upload-file' value=" + v + "></td>"
													+ "</tr>");
							
						});
						$("#selectAll").show();
						$("#unSelectAll").show();
						$("#reverse").show();
						selectAll($(".upload-file"));
						unSelectAll($(".upload-file"));
						reverse($(".upload-file"));
					}
				});

			})
}

// start a task
function startTask() {
	$("#startTask").click(function() {
		var taskName = $("#taskName").val();
		var relationFileName = $(".relation-file:checked").val();
		var relationFilePath=$("#relationPathSelect").val();
		var uploadName = {};
		$(".upload-file:checked").each(function(i){ 
			uploadName[i] = $(this).val(); 
	    }); 
		uploadName=[uploadName];
		var csvFileName=JSON.stringify(uploadName);
		var csvFilePath=$("#uploadPathSelect").val();
		
		debugger
		$.ajax({
			url : "../rest/task/startTask",
			data : {
				"taskName" : taskName,"csvFileName":csvFileName,"csvFilePath":csvFilePath,"relationFileName":relationFileName,"relationFilePath":relationFilePath
			},
			dataType : "text",
			method : "post",
			success : function(data) {
				debugger
				$("#messageReturn").html(data);
			}
		});
	})
}

$(function() {
	addSelectOption();
	viewRelationFile();
	viewUploadFile();
	startTask();
})